package com.zhu.test;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2023/01/30/22:33
 * @Description:
 */
public class AtomicityDemo1 {
    private static volatile int count = 0;

    static Object object = new Object();

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(1);

        for (int i= 0;i<10;i++){
            Thread thread = new Thread(() -> {
                try {
                    countDownLatch.await();
                    for (int i1 = 0; i1 < 1000; i1++) {
                        synchronized (object){
                            count++;
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }

        try{
            Thread.sleep(500);
            countDownLatch.countDown();
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(count);
    }
}
